const 	
        gulp = require('gulp'),
      jshint = require('gulp-jshint'),       // js检查
  	  uglify = require('gulp-uglify'),       // js压缩
  	    sass = require('gulp-sass'),         // sass 编译
  	  cssmin = require('gulp-minify-css'),   // css压缩
fileinclude  = require('gulp-file-include'), // 文件包含
      concat = require('gulp-concat'),       // 文件合并
       group = require('gulp-group-files'),  // 文件组操作
      imgMin = require('gulp-imagemin'),     // 图片压缩
   autofixer = require('gulp-autoprefixer'), // 浏览器兼容
    filecopy = require('gulp-file-copy'), // 浏览器兼容
 loadplugins = require('gulp-load-plugins'),
  sourcemaps = require('gulp-sourcemaps'),
        path = require('path'), 
  livereload = require('gulp-livereload'),
     connect = require('gulp-connect'),		// 路径
      config = require('./gulpfile.config.js');

// ---------------------------- css ---------------------------- //
var sassFiles = {
    "xxx" : {
        src: "src/style/*.scss",
        dest: "build/frontlib/css/"
    }
};

const lad = loadplugins();
const AUTOPREFIXER_BROWSERS = [
    'ie >= 8',
    'ie_mob >= 10',
    'ff >= 26',
    'chrome >= 30',
    'safari >= 7',
    'opera >= 23',
    'ios >= 7',
    'android >= 4.4',
    'bb >= 10'
  ];
gulp.task('sass:compile',function(){
	return group(sassFiles,function (key,fileset){
        return  gulp.src(fileset.src)
                .pipe(sourcemaps.init())
                .pipe(sass({outputStyle:'compact'}))
                // .pipe(sourcemaps.write({includeContent: false}))
                // .pipe(sourcemaps.init({loadMaps: true}))
                .pipe(autofixer({
                   browsers:AUTOPREFIXER_BROWSERS,
                   cascade: true,
                   remove:false
                  }))
                .pipe(sourcemaps.write('.'))
                .pipe(gulp.dest(fileset.dest))
    })();
});

gulp.task("css:min",function(){
	gulp.src('build/frontlib/css/**/*.css')
		.pipe(cssmin())
		.pipe(gulp.dest('dist/frontlib/css'));
});


// ---------------------------- js ---------------------------- //

gulp.task('js:compile', function(){
	gulp.src(['src/bussiness/**/*.js','src/component/**/*.js'])
		.pipe(jshint())
		.pipe(jshint.reporter('default'))
});

gulp.task('js:copybusiness',  function() {
  return gulp.src('src/script/bussiness/**/*.*')
    .pipe(gulp.dest('build/frontlib/js/logic/'))
});

gulp.task('js:copyconfig',  function() {
  return gulp.src('src/script/config/**/*.js')
    .pipe(gulp.dest('build/frontlib/js/logic/config/'))
});

gulp.task('js:copycomp',  function() {
  return gulp.src('src/script/component/*.js')
    .pipe(gulp.dest('build/frontlib/js/logic/common/'))
});

gulp.task('jsconact',function(){
	gulp.src(['src/script/component/*.js'])
		.pipe(concat('common.js'))
		.pipe(gulp.dest('build/frontlib/js/logic/common/'))
});

gulp.task('jsmin', function () {
    gulp.src(['build/frontlib/js/**/*.js'])
        .pipe(uglify({
            mangle:{except: ['require' ,'exports' ,'module' ,'$']},//类型：Boolean 默认：true 是否修改变量名
            compress: true,//类型：Boolean 默认：true 是否完全压缩
            preserveComments: 'all' //保留所有注释
        }))
        .pipe(gulp.dest('dist/frontlib/js'));
});

gulp.task('vendor:copy',function(){
  return gulp.src('src/script/vendor/*.js')
    .pipe(gulp.dest('build/frontlib/js/vendor/'))
});

// ---------------------------- image ---------------------------- //
gulp.task('image:dist', function(){
  gulp.src('src/img/**/*')
    .pipe(imagemin({
      progressive: true,
      interlaced: true
    }))
    .pipe(gulp.dest('dist/frontlib/img'));
});

gulp.task('images:cp',function(){
	gulp.src('src/img/**/*')
		.pipe(gulp.dest('build/frontlib/img/'))
});

// ---------------------------- file include ---------------------------- //
gulp.task('fileinclude', function() {
    gulp.src('src/html/**/*.html')
        .pipe(fileinclude({
          prefix: '@@',
          basepath: '@file'
        }))
    .pipe(gulp.dest('build/html'))
	.pipe(livereload());
});

// --------------------------- deploy ---------------------------- //
gulp.task('developDeploy',function(){
  gulp.src('build/**/*')
    .pipe(gulp.dest(config.deploy));
});

// ---------------------------- watch ---------------------------- //
gulp.task('sass:watch',['sass:compile'],function(){
	gulp.watch('src/style/**/*.scss',['sass:compile']);
});

gulp.task('js:wacth',['js:copybusiness','js:copyconfig','js:copycomp'],function(){
  gulp.watch('src/script/**/*.js',['js:copybusiness','js:copyconfig','js:copycomp'])
});

gulp.task('html:wacth',['fileinclude'],function(){
  gulp.watch('src/html/**/*.html',['fileinclude'])
});

gulp.task('develop',['fileinclude','sass:compile','images:cp','js:compile','js:copybusiness','js:copyconfig',
                      'js:copycomp','vendor:copy']);

gulp.task('watch',['sass:watch','js:wacth','html:wacth','images:cp','fileinclude']);


gulp.task('issueDevelop',['developDeploy']);

//创建一个http服务，并对服务进行配置
gulp.task('serve', function () {
    connect.server({
        port: 9200, //指定端口号，在浏览器中输入localhost:8080就可以直接访问生成的html页面
        root: './build', //指定html文件起始的根目录
        livereload: true //启动实时刷新功能（配合上边的connect.reload()方法同步使用）
    });
});

/* 单文件复制
gulp.task('js:copy', function() {
  var start = 'src/bussiness/';
  gulp.src(start)
      .pipe(filecopy('build/frontlib/js/logic/',{
        start:start
      }))
});
*/